/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.martinkrasa.hockeymanager.site.interfaces;

import com.martinkrasa.hockeymanager.site.entity.Match;
import com.martinkrasa.hockeymanager.site.entity.MatchEvent;
import com.martinkrasa.hockeymanager.site.entity.Result;
import java.util.Random;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 *
 * @author martin.krasa
 */
public final class MatchInterface {  
    
    private static final Logger log = LogManager.getLogger();
    
    public static Match playMatch(Match match) {
        
        Random randomGenerator = new Random();
    
        for (int minute = 0; minute < 60; minute++) {

            log.debug("Minute #{} started", minute + 1);
            
            String comment;
            int period;

            if ((period = (minute % 20)) == 0) {
                comment = "Period #" + Integer.toString(period + 1) + " started.";
                match.getEventList().add(new MatchEvent(minute, MatchEvent.EventType.PERIOD_START, comment));
            }

            double homeOffense_vs_awayDefense = getScoringRate(match.getHomeTeam().getOffense(), match.getAwayTeam().getDefense());
            log.debug("Home attack vs Away defense: rating = {}", homeOffense_vs_awayDefense);
            double homeOffense_vs_awayDefense_random = randomGenerator.nextDouble();
            log.debug("Home attack vs Away defense: random = {}", homeOffense_vs_awayDefense_random);            

            if (homeOffense_vs_awayDefense > homeOffense_vs_awayDefense_random) {
                match.getEventList().add(new MatchEvent(minute, MatchEvent.EventType.GOALHOME, "Home team scored."));
                log.debug("Home team scored!!!"); 
            }

            double awayOffense_vs_homeDefense = getScoringRate(match.getAwayTeam().getOffense(), match.getHomeTeam().getDefense());
            log.debug("Away attack vs Home defense: rating = {}", awayOffense_vs_homeDefense);
            double awayOffense_vs_homeDefense_random = randomGenerator.nextDouble();
            log.debug("Away attack vs Home defense: random = {}", awayOffense_vs_homeDefense_random);

            if (awayOffense_vs_homeDefense > awayOffense_vs_homeDefense_random) {
                match.getEventList().add(new MatchEvent(minute, MatchEvent.EventType.GOALAWAY, "Away team scored."));
                log.debug("Away team scored!!!"); 
            }

            if ((period = ((minute + 1) % 20)) == 0) {
                comment = "Period #" + Integer.toString(period + 1) + " finished.";
                match.getEventList().add(new MatchEvent(minute, MatchEvent.EventType.PERIOD_END, comment));
            }
            
            log.debug("Minute #{} finished", minute + 1);
            
        }

        return match;
    }

    private static double getScoringRate(double offense, double defense) {
        
        double offenseRate = offense / 100;
        double defenseRate = defense / 100;

        double scoringRate = 0.5 * offenseRate / (offenseRate + defenseRate);

        return scoringRate;
    }
    
    public static Result getResult(Match match) {
        
        int homeGoals = 0;
        int awayGoals = 0;
        
        for (MatchEvent event : match.getEventList()) {
            if (event.getEventType() == MatchEvent.EventType.GOALHOME)
                homeGoals++;
            else if (event.getEventType() == MatchEvent.EventType.GOALAWAY)
                awayGoals++;
        }
                
        return new Result(homeGoals, awayGoals);
    }
}
